<?php
namespace Admin\ Controller;
use\ Admin\ Model\ BookModel;
use\ Admin\ Controller\ ToolController;
include "./Common/CheckAdmin.php";
final class BookController {


	//资金捐赠列表
	public function showBook() {
		//实例化UserModel对象
		$bookModelObj = new BookModel();

		//以下为分页的代码
		//每页显示条数
		$pagesize = $GLOBALS[ 'config' ][ 'pagesize' ];
		//获取当前页面
		$page = isset( $_GET[ 'page' ] ) ? $_GET[ 'page' ] : 1;
		//获取开始行号(每一页的第一行应该是这一页页数-1然后乘以每页大小)
		$startrows = ( $page - 1 ) * $pagesize;
		//获取总记录数,数据库rowcount
		$rows = $bookModelObj->rowCount();
		//计算总页数（所有行数，除以每页数量就应该是总行数。但是必须上取整，因为最后一页可能不足$pagesize的数量）
		$pages = ceil( $rows / $pagesize );
		//开始页码
		//结束页码
		$start = $page - 5;
		$end = $page + 4;
		if ( $page <= 6 ) {
			$start = 1;
			$end = 10;
		}
		if ( $page >= $pages - 4 ) {
			$start = $pages - 9;
			$end = $pages;
		}
		if ( $pages < 10 ) {
			$start = 1;
			$end = $pages;
		}

		//分页获取记录
		$arrs = $bookModelObj->fetchBookByPages( $startrows, $pagesize );
		//引入视图文件
		include VIEW_PATH . "bookList.html";
	}

	public function addBookView() {
		include VIEW_PATH . "addBook.html";
	}

	public function addBook() {
		$bookModelObj = new BookModel();
		$name = $_POST[ 'book_name' ];
		$isbn = $_POST[ 'book_isbn' ];
		$inventory = $_POST[ 'book_inventory' ];
		//用该书的ISBN到love_book表去查找，有记录则说明库中已有该书，则在数量字段加上值，若记录中没有，则新增一条love_book记录
		$flag = $bookModelObj->isExistBook( $isbn );
		if ( $flag == 0 ) {
			//新增
			//获取结果集存放到变量
			$row = $bookModelObj->addNewBook( $name, $isbn, $inventory );
			if ( $row == 1 ) {
				echo 200;
			} else {
				echo 500;
			}
		} else {
			//加值
			//获取该书的书籍ID,book_id
			$idarr = $bookModelObj->getBookId( $isbn );
			$book_id = $idarr[ 'book_id' ];
			$row = $bookModelObj->updateBookInventory( $book_id, $inventory );
			if ( $row == 1 ) {
				echo 200;
			} else {
				echo 500;
			}
		}
	}

	//加载编辑书籍视图
	public function editBookView() {
		$id = $_GET[ 'id' ];
		$bookModelObj = new BookModel();
		$arr = $bookModelObj->fetchOne( $id );
		include VIEW_PATH . "editBook.html";
	}

	//编辑
	public function editBook() {
		$id = $_POST[ 'id' ];
		$name = $_POST[ 'book_name' ];
		$isbn = $_POST[ 'book_isbn' ];
		$inventory = $_POST[ 'book_inventory' ];
		$out = $_POST[ 'book_out' ];
		//实例化UserMOdel对象
		$bookModelObj = new BookModel();
		$row = $bookModelObj->editBook( $id, $name, $isbn, $inventory, $out );

		if ( $row == 1 ) {
			echo 200;
		} else {
			echo 500;
		}
	}

	//删除
	public function delBook() {
		$id = $_GET[ 'id' ];
		$bookModelObj = new BookModel();
		$row = $bookModelObj->delBook( $id );
		if ( $row == 1 ) {
			$mes = [
				"code" => 200,
				"msg" => "删除成功<br>",
				"url" => "?c=Book&a=showBook"
			];
			$json = json_encode( $mes );
			echo $json;
		}
	}

	//ISBN 
	public function isbn() {
		define( "DS", DIRECTORY_SEPARATOR ); //目录分隔符
		define( "ROOT_PATH", getcwd() . DS ); //网站根目录
		$pic = $_POST[ 'imgUrl' ];
		include_once ROOT_PATH . '/Frame/Vendor/showapi_sdk/Util/Autoloader.php'; //包含sdk中的自动导入工具类
		$url = "http://route.showapi.com/1129-3";
		$METHOD = "POST";
		$showapi_appid = "1040893"; //替换此值,你可以在这里找到 https://www.showapi.com/console#/myApp
		$showapi_sign = "66a4a65bd5954cd2bdf88ded17751019"; //替换此值,你可以在这里找到 https://www.showapi.com/console#/myApp

		$req = new\ Frame\ Vendor\ showapi_sdk\ Http\ ShowapiRequest( $url, $showapi_appid, $showapi_sign );
		$req->addTextPara( "imgUrl", "$pic" );
		$response = $req->post();


		$arr = $response->getContent();
		$arrs = json_decode( $arr, true );
		$res = $arrs[ 'showapi_res_body' ][ 'retText' ];
		if ( empty( $res ) ) {
			//为空
			echo 500;
		} else {
			//有值
			echo $res;
		}
		//print_r($arr);
		//print_r($response->getContent());
	}

	//plupload上传话题图片并且返回文件路径加名字的方法，！！！这里才是图片上传方法
	public function upImg() {
		$typeArr = array( "jpg", "png", "gif", "jpeg" ); //允许上传文件格式  
		$path = "./Upload/Book/img/"; //上传路径  
		$name = $_FILES[ 'file' ][ 'name' ];
		$size = $_FILES[ 'file' ][ 'size' ];
		$name_tmp = $_FILES[ 'file' ][ 'tmp_name' ];
		if ( empty( $name ) ) {
			echo json_encode( array( "error" => "您还未选择图片" ) );
			exit;
		}
		$type = strtolower( substr( strrchr( $name, '.' ), 1 ) ); //获取文件类型  

		if ( !in_array( $type, $typeArr ) ) {
			echo json_encode( array( "error" => "请上传jpg,png或gif类型的图片！" ) );
			exit;
		}
		if ( $size > ( 1024 * 1024 * 3 ) ) {
			echo json_encode( array( "error" => "图片大小已超过3MB！" ) );
			exit;
		}

		$pic_name = date( YmdHi ) . rand( 10000, 99999 ) . "." . $type; //图片名称  
		$pic_url = $path . $pic_name; //上传后图片路径+名称  
		if ( move_uploaded_file( $name_tmp, $pic_url ) ) { //临时文件转移到目标文件夹  
			//利用Tool中的图片压缩方法将图片压缩一下
			//这个图片压缩实测能不改变比例与清晰度的同时最大程度压缩
			$toolObj = new ToolController();
			$toolObj->compressedImage( $pic_url, $pic_url );
			sleep( 1 );
			echo json_encode( array( "error" => "0", "pic" => $pic_url, "name" => $pic_name ) );
		} else {
			echo json_encode( array( "error" => "上传有误，清检查服务器配置！" ) );
		}

	}


}
?>